<?php

class UserConferenceElementManager extends TModule
{
	// -------------------- basic functions --------------------
	public function addUserConferenceElement($user_id, $conference_element_id)
	{
		$user_conference_element = $this->getUserConferenceElement($user_id, $conference_element_id);
		if($user_conference_element == null)
		{
			$user_conference_element = new UserConferenceElementRecord();
			$user_conference_element->setData($user_id, $conference_element_id);
		
			if($user_conference_element->save())
				return $user_conference_element;
			else
				return null;
		}
		else
			return $user_conference_element;		
	}
	
	public function deleteUserConferenceElement($user_id, $conference_element_id)
	{
		if(UserConferenceElementRecord::finder()->deleteAll('user_id = ? AND conference_element_id = ?', $user_id, $conference_element_id) > 0)
			return true;
		return false;
	}
	
	public function deleteUserConferenceElementByUserId($user_id)
	{
		return UserConferenceElementRecord::finder()->deleteAll('user_id = ?', $user_id);
	}
	
	public function getUserConferenceElement($user_id, $conference_element_id)
	{
		return UserConferenceElementRecord::finder()->find('user_id = ? AND conference_element_id = ?', $user_id, $conference_element_id);
	}
	
	// -------------------- extended functions --------------------
	public function getUserConferenceElementsListByUserId($user_id, $revert = false, $offset = -1, $limit = -1)
	{
		$criteria = new TActiveRecordCriteria();
		$criteria->Condition = 'user_id = :user_id';
		$criteria->Parameters[':user_id'] = $user_id;
		
		if($revert)
			;//$criteria->OrdersBy['registration'] = 'desc';
			
		if($offset < 0 || $offset == null)
			$offset = 0;
		if($limit < 0 || $limit == null)
			$limit = $this->countUserConferenceElements();
			
		$criteria->Offset = $offset;
		$criteria->Limit = $limit;
		
		return UserConferenceElementRecord::finder()->findAll($criteria);
	}
	
	public function getUserConferenceElementsListByConferenceElementId($conference_element_id, $revert = false, $offset = -1, $limit = -1)
	{
		$criteria = new TActiveRecordCriteria();
		$criteria->Condition = 'conference_element_id = :conference_element_id';
		$criteria->Parameters[':conference_element_id'] = $conference_element_id;
		
		if($revert)
			;//$criteria->OrdersBy['registration'] = 'desc';
			
		if($offset < 0 || $offset == null)
			$offset = 0;
		if($limit < 0 || $limit == null)
			$limit = $this->countUserConferenceElements();
			
		$criteria->Offset = $offset;
		$criteria->Limit = $limit;
		
		return UserConferenceElementRecord::finder()->findAll($criteria);
	}
	
	public function hasUserConferenceElement($user_id, $conference_element_id)
	{
		if($this->getUserConferenceElement($user_id, $conference_element_id))
			return true;
		return false;
	}
	
	public function countUserConferenceElements()
	{
		return UserConferenceElementRecord::finder()->count();
	}
	
	public function countUserConferenceElementsByUserId($user_id)
	{
		$criteria = new TActiveRecordCriteria();
		$criteria->Condition = 'user_id = :user_id';
		$criteria->Parameters[':user_id'] = $user_id;
		
		return UserConferenceElementRecord::finder()->count($criteria);
	}
	
	public function countUserConferenceElementsByConferenceElementId($conference_element_id)
	{
		$criteria = new TActiveRecordCriteria();
		$criteria->Condition = 'conference_element_id = :conference_element_id';
		$criteria->Parameters[':conference_element_id'] = $conference_element_id;
		
		return UserConferenceElementRecord::finder()->count($criteria);
	}
}

?>